package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyRedisService;
import cn.wolfcode.wolf2w.redis.service.ITravelRedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.TravelStatisVo;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVo;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

	@Autowired
	private IStrategyRedisService strategyRedisService;
	@Autowired
	private IStrategyService strategyService;
	@Autowired
	private ITravelRedisService travelRedisService;
	@Autowired
	private ITravelService travelService;

	@Autowired
	private StringRedisTemplate template;
	@Autowired
	private IUserInfoService userInfoService;

	@Override
	public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
		// 需要初始化的用户
		List<UserInfo> users = userInfoService.list();
		// 初始化攻略
		strategyRedisDataInit(users);
		// 初始化游记
		travelRedisDataInit(users);
	}

	public void strategyRedisDataInit(List<UserInfo> users){
		System.out.println("------------初始化--strategy--start--------------------------");
		// 需要初始化的数据
		List<Strategy> strategies = strategyService.list();

		// 遍历所有需要初始化的对象
		for (Strategy strategy : strategies) {
			String key = RedisKeys.VERIFY_STRATEGY_VO.join(strategy.getId());
			// 跳过存在的key
			if(template.hasKey(key)){
				continue;
			}
			StrategyStatisVo strategyStatisVo = new StrategyStatisVo();
			// 每一个攻略都对应一个vo
			BeanUtils.copyProperties(strategy,strategyStatisVo);
			strategyStatisVo.setStrategyId(strategy.getId());
			// 把每个vo都存储到redis中
			strategyRedisService.setStrategyStatisVo(strategyStatisVo);
		}

		for (UserInfo user : users) {
			String key = RedisKeys.VERIFY_USER_STRATEGY_FAVOR.join(user.getId());
			if(template.hasKey(key)){
				continue;
			}
			// 获取到当前用户的收藏列表
			List<String> sidList = user.getStrategiesFavor();
			// 设置到redis中
			template.opsForValue().set(key, JSON.toJSONString(sidList));
		}
		System.out.println("------------初始化--strategy--end--------------------------");
	}

	public void travelRedisDataInit(List<UserInfo> users){
		System.out.println("------------初始化--travel--start--------------------------");
		// 需要初始化的数据
		List<Travel> travels = travelService.list();

		// 遍历所有需要初始化的对象
		for (Travel travel : travels) {
			String key = RedisKeys.VERIFY_STRATEGY_VO.join(travel.getId());
			// 跳过存在的key
			if(template.hasKey(key)){
				continue;
			}
			TravelStatisVo travelStatisVo = new TravelStatisVo();
			// 每一个攻略都对应一个vo
			BeanUtils.copyProperties(travel,travelStatisVo);
			travelStatisVo.setTravelId(travel.getId());
			// 把每个vo都存储到redis中
			travelRedisService.setTravelStatisVo(travelStatisVo);
		}

		for (UserInfo user : users) {
			String key = RedisKeys.VERIFY_USER_STRATEGY_FAVOR.join(user.getId());
			if(template.hasKey(key)){
				continue;
			}
			// 获取到当前用户的收藏列表
			List<String> tidList = user.getTravelFavor();
			// 设置到redis中
			template.opsForValue().set(key, JSON.toJSONString(tidList));
		}
		System.out.println("------------初始化--travel--end--------------------------");
	}
}
